<template>
  <div>
    <div class="head" style="width: 100%">
      <div class="name">{{ item.name }}</div>
      <div class="manager">{{ item.manager }}</div>
      <el-dropdown class="dropdown" @command="commandEvent">
        <span>
          <span>操作</span>
          <i class="el-icon-arrow-down el-icon--right" />
        </span>
        <template #dropdown>
          <el-dropdown-menu>
            <el-dropdown-item command="add">添加子部门</el-dropdown-item>
            <el-dropdown-item
              v-if="!item.top"
              command="look"
            >查看部门</el-dropdown-item>
            <el-dropdown-item
              v-if="!item.top"
              command="del"
            >删除部门</el-dropdown-item>
          </el-dropdown-menu>
        </template>
      </el-dropdown>
    </div>
  </div>
</template>

<script>
import { companyDepartmentDelete } from '@/api/department'
export default {
  props: {
    item: {
      type: Object,
      default: () => ({})
    }
  },
  methods: {
    commandEvent(command) {
      switch (command) {
        case 'add':
          this.$bus.$emit('addEvent', true, this.item, 'add')
          console.log('添加')
          break
        case 'look':
          this.$bus.$emit('addEvent', true, this.item, 'edit')
          console.log('查看')
          break
        case 'del':
          this.$confirm('确认要删除吗?', '删除部门')
            .then(async() => {
              const res = companyDepartmentDelete(this.item.id)
              console.log(res)
              this.$message.success('删除成功')
              this.$emit('getData')
            })
            .catch()
          console.log('删除')
          break
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.head {
  display: flex;
  align-items: center;
  .name {
    flex: 1;
  }
  .manager {
    width: 120px;
    margin-right: 20px;
    text-align: right;
  }
}
</style>
